package com.xvaca.spcms.servlet.admin.partner;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xvaca.spcms.bean.PartnerBean;
import com.xvaca.spcms.dao.PartnerDAO;

/**
 * Servlet implementation class PartnerEditServlet
 */
public class PartnerEditServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String id = request.getParameter("id");
		
		PartnerDAO dao = new PartnerDAO();
		request.setAttribute("partner", dao.getPartnerById(Integer.parseInt(id)));
		request.getRequestDispatcher("partner_edit.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
		
		String id = request.getParameter("id");
		String name = request.getParameter("name"); // 合作方名称
		String contact = request.getParameter("contact"); // 联系人
		String cycle = request.getParameter("cycle"); // 结算周期
		String account = request.getParameter("account"); // 账户
		String status = request.getParameter("status"); // 状态
		
		PartnerBean partner = new PartnerBean();
		partner.setId(Integer.parseInt(id));
		partner.setAccount(account);
		partner.setContact(contact);
		partner.setCycle(Integer.parseInt(cycle));
		partner.setName(name);
		partner.setStatus(Integer.parseInt(status));
		partner.setUpdatedAt(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		
		PartnerDAO dao = new PartnerDAO();
		dao.updatePartner(partner);
		
		response.sendRedirect("partnerList");
	}

}
